﻿using System;

namespace Smart.CE.Log
{
    /// <summary>
    /// 
    /// </summary>
    public abstract class LogListener : IDisposable 
    {
        private bool needIndent = true;
        private int indentLevel;
        private int indentSize = 4;

        /// <summary>
        /// 
        /// </summary>
        protected bool NeedIndent
        {
            get
            {
                return this.needIndent;
            }
            set
            {
                this.needIndent = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public int IndentLevel
        {
            get { return this.indentLevel; }
            set { this.indentLevel = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public int IndentSize
        {
            get { return this.indentSize; }
            set { this.indentSize = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public void Dispose()
        {
            this.Dispose( true );
            GC.SuppressFinalize( this );
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="disposing"></param>
        protected virtual void Dispose(bool disposing)
        {
        }

        /// <summary>
        /// 
        /// </summary>
        public virtual void Close()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        public virtual void Flush()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        public abstract void Write(string message);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        public abstract void WriteLine(string message);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="o"></param>
        public virtual void Write(object o)
        {
            if( o != null )
            {
                Write( o.ToString() );
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="o"></param>
        public virtual void WriteLine(object o)
        {
            this.WriteLine( ( o == null ) ? "" : o.ToString() );
        }

        /// <summary>
        /// 
        /// </summary>
        protected virtual void WriteIndent()
        {
            NeedIndent = false;

            for( int i = 0; i < this.indentLevel; i++ )
            {
                if( this.indentSize == 4 )
                {
                    Write( "    " );
                }
                else
                {
                    for( int j = 0; j < this.indentSize; j++ )
                    {
                        Write( " " );
                    }
                }
            }
        }
    }
}
